package com.university;

import static org.junit.Assert.*;

import java.util.Collection;
import java.util.Iterator;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Before;
import org.junit.Test;

public class TestUniversity {

	KnowledgeBase kbase;
	StatefulKnowledgeSession ksession;

	@Before
	public void setUp() throws Exception {
		this.kbase = readKnowledgeBase();
		this.ksession = kbase.newStatefulKnowledgeSession();
		FakeDBInterface fakeDB=new FakeDBInterface();
		ksession.insert(fakeDB);
	}

	public void tearDown() {
		this.ksession.dispose();
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("University.drl"),
				ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error : errors) {
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}

	@Test
	public void testCountingLectures() {
		Professor professor=new Professor("Harri Airaksinen");
		ksession.insert(professor);
		
		ksession.fireAllRules();
		System.out.println(professor);
		if(professor.getLectureHours()!=8)
		{
			fail("Failed at counting hours");
		}
		
	}
}